//
//  RCChangeRecordDetailViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-25.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCChangeRecordDetailViewController.h"
#import "RCMerchantCommentListDataModel.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSArray+EX.h"
#import "FrameworkRelativeDefines.h"
#import "RCMerchantDetailWriteCommentViewController.h"
#import "GloableEngineService.h"

#define kRightButtonImageFile    @"image_merchant_comment_rightButton.png"
#define kRn                  15

@interface RCChangeRecordDetailViewController ()<MerchantWriteCommentDelegate>

@property (nonatomic, retain) RCMerchantCommentListDataModel *dataModel;
@property (nonatomic, retain) MBProgressHUDEx *progressHub;
@property (nonatomic, assign) int64_t requestID;
@property (nonatomic, assign) BOOL isRefresh;
@property (nonatomic, assign) NSInteger pn;
@property (nonatomic, copy)   NSString *shopID;
@property (nonatomic, copy)   NSString *shopName;
@property (nonatomic, retain) UIButton *rightButton;
@property (nonatomic, retain) NSMutableArray *tabelDataArray;

@end

@implementation RCChangeRecordDetailViewController

@synthesize dataModel = _dataModel;
@synthesize progressHub = _progressHub;
@synthesize requestID = _requestID;
@synthesize isRefresh = _isRefresh;
@synthesize pn = _pn;
@synthesize shopID = _shopID;
@synthesize shopName = _shopName;
@synthesize rightButton = _rightButton;
@synthesize tabelDataArray = _tabelDataArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithShopId:(NSString *)shopID withShopName:(NSString *)shopName{
    self = [super init];
    if (self) {
        self.shopID = shopID;
        self.shopName = shopName;
        _tabelDataArray = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void) dealloc
{
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
    _requestID = kInvalidRequestId;
    self.dataModel = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.shopID = nil;
    self.shopName = nil;
    self.rightButton = nil;
    self.tabelDataArray = nil;
    [super dealloc];
}

-(void)loadView{
    [super loadView];
    
//    _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
//    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
//                  forState:UIControlStateNormal];
//    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
//                  forState:UIControlStateHighlighted];
//    [_rightButton addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [self createLeftBarButtonItemWithButton:_rightButton];
    
    self.navigationItem.titleView = [self createTwolineTitleView];
    
}

-(UIView *)createTwolineTitleView{
    UIView *titleView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 44)]autorelease];
    UILabel *upperLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 150, 15)];
    upperLabel.backgroundColor = [UIColor clearColor];
    upperLabel.textColor = [UIColor whiteColor];
    upperLabel.textAlignment = UITextAlignmentCenter;
    upperLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    upperLabel.shadowColor = [UIColor colorWithHex:0xff2c2c2];
    upperLabel.shadowOffset = CGSizeMake(0, 0.5);
    upperLabel.text = @"兑换记录";
    [titleView addSubview:upperLabel];
    [upperLabel release];
    
    UILabel *downLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 24, 150, 15)];
    downLabel.backgroundColor = [UIColor clearColor];
    downLabel.textColor = [UIColor whiteColor];
    downLabel.textAlignment = UITextAlignmentCenter;
    downLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    downLabel.shadowColor = [UIColor colorWithHex:0xff2c2c2];
    downLabel.shadowOffset = CGSizeMake(0, 0.5);
    downLabel.text = self.shopName;
    [titleView addSubview:downLabel];
    [downLabel release];
    return titleView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20);
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
    }
    [self refreshData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

-(void)rightButtonClicked{
    
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCMerchantDetailWriteCommentViewController *vc = [[RCMerchantDetailWriteCommentViewController alloc]initWithMerchant:self.shopID withName:self.shopName];
    vc.delegate = (id<MerchantWriteCommentDelegate>)self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

#pragma mark tableview
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tabelDataArray count];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    RCMerchantCommentListItem *item = [_tabelDataArray safeObjectAtIndex:row];
    if (nil == item) {
        return 0;
    }
    CGFloat cellHeight = [RCMerchantCommentCell getCellHightWithContent:item.content withIsExpand:item.openState];
    return cellHeight;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MerchantCommentCell";
    RCMerchantCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[[RCMerchantCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
        cell.delegate = self;
    }
    NSInteger row = [indexPath row];
    cell.getMoreBtn.tag = row;
    RCMerchantCommentListItem *item = [_tabelDataArray safeObjectAtIndex:row];
    [cell bindDataWithContentStr:item withIsExpand:item.openState];
    return cell;
}

- (void)  tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(void) loadMoreData
{
    _isRefresh = NO;
    
    _requestID = [[IManager sharedInstance] requestChangeRecordDetailInfo:self.shopID withDelegate:self];
    
}



#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    
    _isRefresh = YES;
    _pn = 0;
    _requestID = [[IManager sharedInstance] requestChangeRecordDetailInfo:self.shopID withDelegate:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        if (_isRefresh) {
            //下拉刷新
            [_tabelDataArray removeAllObjects];
            self.dataModel = (RCMerchantCommentListDataModel *)dataModel;
            if (self.dataModel != nil && self.dataModel.commentItemList != nil) {
                
                for (int i =0; i < [self.dataModel.commentItemList count]; i++) {
                    RCMerchantCommentListItem *item = [self.dataModel.commentItemList safeObjectAtIndex:i];
                    item.content = [NSString stringWithFormat:@"用户%@兑换了一部",item.userName];
                }
            }
            
            [_tabelDataArray addObjectsFromArray:self.dataModel.commentItemList];
            
            [self finishRefreshingData];
        }else{
            //加载更多的
            RCMerchantCommentListDataModel *newData = (RCMerchantCommentListDataModel *)dataModel;
            
            [_tabelDataArray addObjectsFromArray:newData.commentItemList];
            
            [baseVCTableView reloadData];
            [self finishLoadMoreData];
        }
        
        [self noMoreData];
    }
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
    
}

#pragma CommentCellDelegate

-(void)getMoreButtonClicked:(NSInteger)tag{
    RCMerchantCommentListItem *item = [_tabelDataArray safeObjectAtIndex:tag];
    item.openState = !item.openState;
    [baseVCTableView reloadData];
}

#pragma WriteCommentDelegate

-(void)successWriteComment{
    [self refreshData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
